<?php

class MstClientController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column1';

		

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new MstClient;
		$model->createdby = Yii::app()->user->getId();
		

		if (isset($_POST['MstClient'])) {
			$acc = $_POST['account_number'];
			$acc_name = $_POST['account_name'];
			$bank = $_POST['bank_name'];
			$branch = $_POST['branch'];
			$currency = $_POST['currency'];

			$model->attributes=$_POST['MstClient'];

			$chk = substr(MstClient::model()->find(array('order'=>'id desc'))->client_number,11)+1;

			$numb = str_pad($chk, 4, "0", STR_PAD_LEFT); 
			$generate = 'CLI'.date('Ymd').$numb;
			$model->client_number = $generate;
			if ($model->save()) {
				MstClient::model()->updateByPk($model->id, array("groups"=> $model->id));
				for($i=0;$i<count($acc);$i++){
					$model2 = new MstClientAccount;
					$model2->mst_corporate_id = $model->id;
					$model2->account_number = $acc[$i];
					$model2->account_name = $acc_name[$i];
					$model2->bank_name = $bank[$i];
					$model2->branch = $branch[$i];
					$model2->currency_id = $currency[$i];
					$model2->createdby = Yii::app()->user->getId();
					$model2->save();
				}
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}
	
	
	public function actionCreateqq($parent)
	{
		$model=new MstClient;
		$model->createdby = Yii::app()->user->getId();
		$model->parent_id = $parent;
		$model->groups = $this->loadModel($parent)->groups;
		
		//$this->layout = false;
		//var_dump($_POST['account_number']);
		$acc = $_POST['account_number'];
		$bank = $_POST['bank_name'];
		$branch = $_POST['branch'];
		$currency = $_POST['currency'];
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['MstClient'])) {
			$model->attributes=$_POST['MstClient'];
			if ($model->save()) {
				for($i=0;$i<count($acc);$i++){
					$model2 = new MstClientAccount;
					$model2->mst_corporate_id = $model->id;
					$model2->account_number = $acc[$i];
					$model2->bank_name = $bank[$i];
					$model2->branch = $branch[$i];
					$model2->currency_id = $currency[$i];
					$model2->createdby = Yii::app()->user->getId();
					$model2->save();
				}
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('createqq',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['MstClient'])) {
			$id_account = $_POST['id_account'];
			$acc = $_POST['account_number'];
			$acc_name = $_POST['account_name'];
			$bank = $_POST['bank_name'];
			$branch = $_POST['branch'];
			$currency = $_POST['currency'];

			$model->attributes=$_POST['MstClient'];
			if ($model->save()) {
				MstClient::model()->updateByPk($model->id, array("groups"=> $model->id));
				for($i=0;$i<count($acc);$i++){
					$model2 = MstClientAccount::model()->findByPk($id_account[$i]);
					$model2->mst_corporate_id = $model->id;
					$model2->account_number = $acc[$i];
					$model2->account_name = $acc_name[$i];
					$model2->bank_name = $bank[$i];
					$model2->branch = $branch[$i];
					$model2->currency_id = $currency[$i];
					$model2->createdby = Yii::app()->user->getId();
					$model2->save();
				}
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if (!isset($_GET['ajax'])) {
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
			}
		} else {
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('MstClient');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new MstClient('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['MstClient'])) {
			$model->attributes=$_GET['MstClient'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return MstClient the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=MstClient::model()->findByPk($id);
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param MstClient $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='mst-client-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}